﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
\* ######################################## */
using System;
using System.Timers;

namespace AJRavindiran.Jolt.RuneScape.Events
{
    /// <summary>
    /// Represents management for events.
    /// </summary>
    public class EventManager
    {
        #region Methods
        /// <summary>
        /// Submit an event using an event instance.
        /// </summary>
        /// <param name="e">The event to handle.</param>
        public void SubmitEvent(Event e)
        {
            SubmitEvent(e.Interval, e.Iterations, e.Execute);
        }

        /// <summary>
        /// Submit an event using the interval and handler.
        /// </summary>
        /// <param name="interval">The interval between each occurance.</param>
        /// <param name="handler">The method to occur every every interval.</param>
        public void SubmitEvent(long interval, ElapsedEventHandler handler)
        {
            this.SubmitEvent(interval, 0, handler);
        }

        /// <summary>
        /// Submit an event using the interval, iterations and handler.
        /// </summary>
        /// <param name="interval">The interval between each occurance.</param>
        /// <param name="iterations">How many times this timer will occur.</param>
        /// <param name="handler">The method to occur every every interval.</param>
        public void SubmitEvent(long interval, int iterations, ElapsedEventHandler handler)
        {
            var timer = new Timer(interval);
            timer.Elapsed += handler;
            if (iterations > 0)
                timer.Elapsed += (s, e) =>
                {
                    if (--iterations <= 0)
                        timer.Dispose();
                };
            timer.Start();
        }
        #endregion Methods
    }
}
